package com.handpay.zztong.hp;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import com.handpay.framework.utils.CommonUtils;
import com.handpay.framework.utils.DateUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.paymentbill.model.Bill;
import com.handpay.zztong.hp.paymentbill.activity.QueryBillsActivity;

/**
 * 收款后的交易结果（交易失败或者处理中）
 *
 * @author 杨娟娟
 */
public class TradingResults extends ZZTongActivity {
    public static final String KEY_NAME = "card_in_name";
    private TextView tv_trans_Amount;
    private TextView tv_cardNoOut;
    private TextView tv_transaction_status;
    private TextView transTime;
    private TextView transSn;
    private TextView tv_trans_type;
    private TextView tv_cardNoIn;
    private TextView tv_cardNoIn_name;
    private TextView tv_Transfer_accounts_Amount;
    private TextView tv_Transfer_Fee;
    private TableRow tr_Transfer_accounts_Amount;
    private TableRow tr_Transfer_Feen;
    private TextView tv_failed;
    //private LinearLayout ll_failed;
    private TableRow tr_failed;
    private ImageView success_stmap;
    private TableRow tr_name;
    private Button nextButton;
    private Bill mBill = null;


    @Override
    public ActionBar getActivityActionBar() {
        //leftBar goBack();
        return new ActionBar(this.getString(R.string.super_transfer), true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.trans_super_successed_bill);
        super.onCreate(savedInstanceState);
        this.mAction = getIntent().getByteExtra(ACTION, Action_QueryBills);
        Intent intent = getIntent();
        mBill = (Bill) intent.getSerializableExtra(QueryBillsActivity.billvalue);

        tv_trans_Amount = (TextView) findViewById(R.id.tv_trans_Amount);
        tv_cardNoOut = (TextView) findViewById(R.id.tv_cardNoOut);
        tv_transaction_status = (TextView) findViewById(R.id.tv_transaction_status);
        transTime = (TextView) findViewById(R.id.transTime);
        transSn = (TextView) findViewById(R.id.transSn);
        tv_trans_type = (TextView) findViewById(R.id.tv_trans_type);
        tv_cardNoIn = (TextView) findViewById(R.id.tv_cardNoIn);
        tv_cardNoIn_name = (TextView) findViewById(R.id.tv_cardNoIn_name);
        tv_Transfer_accounts_Amount = (TextView) findViewById(R.id.tv_Transfer_accounts_Amount);
        tv_Transfer_Fee = (TextView) findViewById(R.id.tv_Transfer_Fee);
        tr_Transfer_accounts_Amount = (TableRow) findViewById(R.id.tr_Transfer_accounts_Amount);
        tr_Transfer_Feen = (TableRow) findViewById(R.id.tr_Transfer_Feen);
        tr_failed = (TableRow) findViewById(R.id.tr_failed);
        tv_failed = (TextView) findViewById(R.id.tv_failed);
        nextButton = (Button) findViewById(R.id.nextButton);
        //ll_failed=(LinearLayout)findViewById(R.id.ll_failed);
        success_stmap = (ImageView) findViewById(R.id.success_stmap);
        tr_name = (TableRow) findViewById(R.id.tr_name);
        nextButton.setOnClickListener(this);
        Initialization();
        TextView repayAmount = (TextView) findViewById(R.id.label_amount);
        if (mAction == Action_CreditCardRepay) {
            myActionBar.setTitleText(R.string.repayment_Creditcard);
            repayAmount.setText(R.string.repayment_amount);
        } else if (mAction == Action_SuperUniTrans) {
            myActionBar.setTitleText("交易详情");
            byte action_from = this.getIntent().getByteExtra(ACTION_FROM, (byte) 0);
            if (action_from == Action_FROM_CREDIT) {
                repayAmount.setText(R.string.repayment_amount);
            }
        }

    }

    protected boolean isShownTabs() {
        return false;
    }

    /***
     * 初始化基本信息
     */
    private void Initialization() {
        if (mBill != null) {
            tv_cardNoIn_name.setText(getIntent().getStringExtra(KEY_NAME));
            tv_trans_Amount.setText(CommonUtils.formatMoney((mBill.getAmount() + mBill.getFee()) / 100));
            tv_cardNoOut.setText(mBill.getCardNoOut());
            transTime.setText(DateUtils.formatDateToString(DateUtils
                    .formatStringToDate(mBill.getTransTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
            transSn.setText(mBill.getTransSN());
            tv_cardNoIn.setText(mBill.getCardNoIn());
            if (mBill.getStatus() == Bill.STATUS_SUCCESS) {
                findViewById(R.id.iv_transfer_status).setBackgroundResource(R.drawable.transfer_success);
                ((TextView) this.findViewById(R.id.BusinessInformationText11)).setText(R.string.trans_success);
                tv_transaction_status.setText(mBill.getTransStatus());
                success_stmap.setBackgroundResource(R.drawable.successstamp);
                tr_Transfer_accounts_Amount.setVisibility(View.VISIBLE);
                tr_Transfer_Feen.setVisibility(View.VISIBLE);
                tr_failed.setVisibility(View.GONE);
            } else if (mBill.getStatus() == Bill.STATUS_FAILED) {
                findViewById(R.id.iv_transfer_status).setBackgroundResource(R.drawable.transfer_fail);
                ((TextView) this.findViewById(R.id.BusinessInformationText11)).setText(R.string.trans_fail);
                tv_transaction_status.setText(R.string.trans_fail);
                success_stmap.setBackgroundResource(R.drawable.failstamp);
                tv_failed.setText(mBill.getTransStatus());
                tr_Transfer_accounts_Amount.setVisibility(View.GONE);
                tr_Transfer_Feen.setVisibility(View.GONE);
                tr_failed.setVisibility(View.VISIBLE);
            } else if (mBill.getStatus() == Bill.STATUS_PROCESSING) {
                findViewById(R.id.iv_transfer_status).setVisibility(View.GONE);
                findViewById(R.id.tv_label_failed).setVisibility(View.GONE);
                ((TextView) this.findViewById(R.id.BusinessInformationText11)).setText(R.string.trans_processing);
                tv_transaction_status.setText(R.string.trans_processing);
                success_stmap.setVisibility(View.GONE);
                tv_failed.setText(mBill.getTransStatus());
                tr_Transfer_accounts_Amount.setVisibility(View.GONE);
                tr_Transfer_Feen.setVisibility(View.GONE);
                tr_failed.setVisibility(View.VISIBLE);
            }
            if (!TextUtils.isEmpty(mBill.getTransChannel()) && mBill.getTransChannel().equals("2")) {
                byte action_from = this.getIntent().getByteExtra(ACTION_FROM, (byte) 0);
                if (action_from == Action_FROM_CREDIT) {
                    tv_trans_type.setText("信用卡还款");
                }else{
                    tv_trans_type.setText(R.string.super_unipay);
                }

                tv_Transfer_Fee.setText(R.string.str_uninpay_fee);
//                tv_Transfer_accounts_Amount.setText(R.string.str_uninoay_amount);
                tr_name.setVisibility(View.GONE);
            } else {
                if (mAction == Action_CreditCardRepay) {
                    tv_trans_type.setText(R.string.repayment_Creditcard);
                } else {
                    tv_trans_type.setText(R.string.super_transfer);
                }
                tv_Transfer_Fee.setText(CommonUtils.formatMoney(mBill.getFee() / 100));
                tr_name.setVisibility(View.VISIBLE);
            }
            tv_Transfer_accounts_Amount.setText(CommonUtils.formatMoney(mBill.getAmount() / 100));
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.nextButton) {
            this.finish();

        }
    }

    @Override
    public void onSwiperPlugOff() {
        // 回到HPFunction界面
        onFunctionTab(null);
    }
}
